package com.kazu.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author kazu
 * @Date 2023/2/18 13:56
 * @Version 1.0
 */
@SuppressWarnings({"all"})
public class CorsConfig {
	
	@Bean
	public CorsWebFilter corsWebFilter() {
		
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		
		// 配置跨域
		corsConfiguration.addAllowedHeader("*");	/*请求头*/
		corsConfiguration.addAllowedMethod("*");	/*请求方法*/
//		corsConfiguration.addAllowedOrigin("*");	/*请求来源*/
		/*springboot>2.4.3使用addAllowedOriginPattern*/
		corsConfiguration.addAllowedOriginPattern("*");	/*请求来源*/
		corsConfiguration.setAllowCredentials(true);	/*携带cookie信息*/
		
		source.registerCorsConfiguration("/**", corsConfiguration);
		return new CorsWebFilter(source);
	}
	
}
